<?php

namespace App\Store\Controllers\Financial\Statements;

use App\Common\Services\Store\Common as StoreCommon;
use App\Http\Controllers\Controller;
use App\Store\Models\Financial\Statements\Subject;
use Illuminate\Http\Request;
use App\Store\Validate\Financial\Statements\Subject as ValidateSubject;
use App\Common\Services\CommonExcelExport;
use Maatwebsite\Excel\Facades\Excel;

/**
 * 科目
 */
class SubjectController extends Controller
{
    public function dopostadd(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        if (!$request->serial_number) {
            return response()->json(['code' => '001', 'msg' => '科目编码不能为空']);
        }
        if (!$request->subject_name) {
            return response()->json(['code' => '001', 'msg' => '科目名称不能为空']);
        }

        $serial_number = (string)$request->serial_number;
        $length = strlen($serial_number);
        if ($length >= 4) {
            $upper_number = substr($serial_number, 0, 4);
        } else {
            return response()->json(['code' => '001', 'msg' => '科目编码不能小于4位数']);
        }

        $info = Subject::query()
            ->where(function ($query) use ($cp_id) {
                $query->where('cp_id', $cp_id)
                    ->orWhere('cp_id', 0);
            })
            ->where('serial_number', $serial_number)
            ->where('is_del', 1)
            ->first();
        $info =empty($info)? []:$info->toArray();
        if (!empty($info)) {
            return response()->json(['code' => '001', 'msg' => '科目编码已存在']);
        }


        $moneylog_data = [
            'cp_id' => $cp_id,
            // 'store_id' => $req_data['store_id'],
            'subject_name' => $request->subject_name,
            'serial_number' => $serial_number,
            'upper_number' => $upper_number,
            'is_initial' => 2,
            'create_at' => time(),
            'update_at' => time(),
        ];
        Subject::query()->create($moneylog_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Subject::getList($request);
        //增加预制的数据
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {

        $id = $request->id ?? 0;
        $one = Subject::query()
            ->where('id', $id)
            ->where('is_del', 1)
            ->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = (int)$accountinfo['cp_id'];
        $id = $request->id ?? 0;
        if (!$request->serial_number) {
            return response()->json(['code' => '001', 'msg' => '科目编码不能为空']);
        }
        if (!$request->subject_name) {
            return response()->json(['code' => '001', 'msg' => '科目名称不能为空']);
        }

        $one = Subject::query()->where('id', $id)->where('is_del', 1)->first();
        if (empty($one)) {
            return response()->json(['code' => '001', 'msg' => '数据不存在']);
        }

        $serial_number = (string)$request->serial_number;
        $length = strlen($serial_number);
        if ($length >= 4) {
            $upper_number = substr($serial_number, 0, 4);
        } else {
            return response()->json(['code' => '001', 'msg' => '科目编码不能小于4位数']);
        }

        $info = Subject::query()
            ->where(function ($query) use ($cp_id) {
                $query->where('cp_id', $cp_id)
                    ->orWhere('cp_id', 0);
            })
            ->where('serial_number', $serial_number)
            ->where('id', '<>', $id)
            ->first();
        if (!empty($info)) {
            return response()->json(['code' => '001', 'msg' => '科目编码已存在']);
        }

        Subject::query()
            ->where('id', $id)
            ->update([
                'subject_name' => $request->subject_name,
                'serial_number' => $serial_number,
                'upper_number' => $upper_number,
                'update_at' => time()
            ]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dopostdel(Request $request)
    {
        $id = $request->id ?? 0;
        //$accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $one = Subject::query()->where('id', $id)->first();
        if (!$one) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $one = $one->toArray();

        if ($one['is_initial'] == 1) {
            return response()->json(['code' => '001', 'msg' => '该科目不允许删除']);
        }

        Subject::where('id', $id)->update(['is_del' => 2, 'del_at' => time()]);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }


    /**
     * 导出
     */
    public function doexport(Request $request)
    {
        $list = Subject::getList($request)['list'];

        $exceldata = [];
        $row = [
            ['id' => '科目管理'],
            [
                'id' => '序号',
                'serial_number' => '科目编号',
                'subject_name' => '科目名称',
            ]
        ];
        foreach ($list as $key => $val) {
            $exceldata[] = [
                'id' => $key + 1,
                'serial_number' => $val['serial_number'],
                'subject_name' => $val['subject_name'],
            ];
        }

        $filename = '科目信息导出_' . time() . '.xlsx';
        //不需要改
        return Excel::download(new CommonExcelExport($row, $exceldata, [], [], false), $filename);
    }
}
